//
//  ZXHomeHeadView.m
//  ZiJinLian
//
//  Created by 震霄 张 on 2018/3/11.
//  Copyright © 2018年 lzy. All rights reserved.
//

#import "ZXHomeHeadView.h"
#import "HomeAdverModel.h"
#import "HomeSonClassModel.h"
#import "HomeChoiceView.h"
@interface ZXHomeHeadView()<SDCycleScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UILabel *subTitleLabel;
@property (weak, nonatomic) IBOutlet UIScrollView *choiceScrollView;
@property (weak, nonatomic) IBOutlet UIView *headView;

@property (nonatomic,weak)SDCycleScrollView *topAdvView;

@end

@implementation ZXHomeHeadView

+(instancetype)zxhomeHeadView
{
	return [[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil].lastObject;

}
-(void)awakeFromNib
{
	[super awakeFromNib];
	self.autoresizingMask = UIViewAutoresizingNone;
	SDCycleScrollView *sectionScrollerView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(25, CGRectGetMaxY(self.headView.frame), MainW-50, (MainW-50)*0.4) delegate:self placeholderImage:[UIImage imageNamed:@"banner_bj"]];
	sectionScrollerView.bannerImageViewContentMode=UIViewContentModeScaleAspectFill;
	//	sectionScrollerView.imageURLStringsGroup = @[@"banner_bj",@"fo"];  // 网络图片
	//self.sectionScrollerView.localizationImageNamesGroup =array1 ; // 本地图片
	sectionScrollerView.pageControlAliment = SDCycleScrollViewPageContolAlimentCenter;
	sectionScrollerView.autoScrollTimeInterval = 3.;
	[self addSubview:sectionScrollerView];
	self.topAdvView = sectionScrollerView;
	
}
- (IBAction)checkMoreBtnClick:(id)sender {
	NSLog(@"check more");
	NSMutableDictionary *dic = [NSMutableDictionary dictionary];
	[dic setValue:@"4" forKey:@"type"];
	[dic setValue:[NSString stringWithFormat:@"%ld",self.model.cateId] forKey:@"value"];
	[[NSNotificationCenter defaultCenter]postNotificationName:@"jumpOther" object:nil userInfo:dic];
	
}
-(void)setModel:(HomeSuggestModel *)model
{
	_model = model;
	NSMutableArray *tmpArr = [NSMutableArray array];
	for (int i = 0; i<model.adverts.count; i++) {
		HomeAdverModel *am = model.adverts[i];
		[tmpArr addObject:am.imageUrl];
	}
	self.topAdvView.imageURLStringsGroup = [tmpArr copy];
	self.titleLabel.text = model.cateName;
	self.subTitleLabel.text = model.subtitle;
	
	[self.choiceScrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
	CGFloat choiceW = (MainW-50)/6;

	self.choiceScrollView.contentSize = CGSizeMake(choiceW * model.children.count, 40);
	for (int j = 0; j<model.children.count; j++) {
		HomeSonClassModel *sm = model.children[j];
		HomeChoiceView *choiceView = [HomeChoiceView homechoiceView];
		choiceView.model = sm;
		choiceView.LastOne = j==model.children.count-1 ? YES:NO;
		choiceView.frame = CGRectMake(j*choiceW, 0, choiceW, 40);
		[self.choiceScrollView addSubview:choiceView];
	}
	
	
}
/** 点击图片回调 */
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index
{
	//跳转网页
	HomeAdverModel *advModel = self.model.adverts[index];
	NSString *url = advModel.linkUrl;
	if (url.length>0) {
		NSMutableDictionary *dic = [NSMutableDictionary dictionary];
		[dic setValue:@"1" forKey:@"type"];
		[dic setValue:url forKey:@"value"];
		[[NSNotificationCenter defaultCenter]postNotificationName:@"jumpOther" object:nil userInfo:dic];
	}
}

/** 图片滚动回调 */
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didScrollToIndex:(NSInteger)index
{
	
}
@end
